package indi.kider.server.woniubus.api;

import indi.kider.common.constant.EmployeeRoleCons;
import indi.kider.common.model.bill.RentBill;
import indi.kider.common.model.employee.Employee;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.server.woniubus.service.ExcelService;
import indi.kider.server.woniubus.service.related.AuthcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * excel相关
 */
@RestController
@RequestMapping(value = "excel")
public class ExcelController {

    @Autowired
    private ExcelService excelService;
    @Autowired
    private AuthcService authcService;

    /**
     * @api {POST} woniubus/excel/exportTenant
     * @apiGroup  woniubus
     * @apiDescription 租客导出
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/excel/exportTenant
     * query":{"communityId":-1,"housesId":-1,"billType":-1,"payState":-1,"expression":""}
     * @apiSuccessExample {json} 返回样例:
     * 文件
     **/
    @PostMapping(value = "exportTenant")
    public ResponseEntity<InputStreamResource> exportTenant(@RequestBody TenantQuery tenantQuery) throws IOException {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            tenantQuery.setEnterpriseId(employee.getEnterpriseId());
        }
        return excelService.exportTenant(tenantQuery);
    }

    /**
     * @api {POST} woniubus/excel/exportBillByHouse
     * @apiGroup woniubus
     * @apiDescription 查询账单导出
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/excel/exportBillByHouse
     * @apiSuccessExample {json} 返回样例:
     * 文件
     **/
    @PostMapping(value = "exportBillByHouse")
    public ResponseEntity<InputStreamResource> exportBillByHouse(@RequestBody RentBill rentBill) throws IOException {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            rentBill.setEnterpriseId(employee.getEnterpriseId());
        }
        return excelService.exportBillByHouse(rentBill);
    }

    /**
     * @api {POST} woniubus/excel/exportBillByTenant
     * @apiGroup woniubus
     * @apiDescription 查询账单导出
     * @apiParamExample 请求样例：
     * http://localhost:9002/woniubus/excel/exportBillByTenant
     * @apiSuccessExample {json} 返回样例:
     * 文件
     **/
    @PostMapping(value = "exportBillByTenant")
    public ResponseEntity<InputStreamResource> exportBillByTenant(@RequestBody RentBill rentBill) throws IOException {
        Employee employee = authcService.getCurrentUser().getData();
        if (employee != null && !employee.getRoles().equals(EmployeeRoleCons.ADMIN)) {//超级管理员
            rentBill.setEnterpriseId(employee.getEnterpriseId());
        }
        return excelService.exportBillByTenant(rentBill);
    }
}
